package com.gxa.hotel.portal.controller;

import com.gxa.hotel.core.common.JsonResult;
import com.gxa.hotel.core.exception.BusinessException;
import com.gxa.hotel.core.exception.ExceptionCode;
import com.gxa.hotel.core.pojo.domain.PayDetail;
import com.gxa.hotel.core.pojo.domain.RoomOrder;
import com.gxa.hotel.core.pojo.domain.RoomState;
import com.gxa.hotel.portal.service.HotelCacheService;
import com.gxa.hotel.portal.service.RoomsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/roomCache")
@Api
@RestController
public class RoomsCacheController {

    @Autowired
    RoomsService roomsService;

    @Autowired
    HotelCacheService hotelCacheService;

    @ApiOperation("选择日期后,酒店当天的房型列表")
    @PostMapping("/list")
    public Map selectList(String[] date,Integer size,Integer page) throws BusinessException {
        List<RoomState> roomStates = roomsService.selectList(date);

        String s = hotelCacheService.selectDetail();

        int size1 = roomStates.size();
        int start= page * (size - 1);
        int end=page*size-1;

     List list=new ArrayList();
        if(start>size1){
            throw new BusinessException(ExceptionCode.WRONG_PAGE);
        }

        if(start>0&&start<=size1){
            if(end>=size1){
                for (int i =start; i <size1; i++) {
                    list.add(roomStates.get(i));
                }
            }else{
                for (int i = start; i < end; i++) {
                    list.add(roomStates.get(i));
                }
            }
        }
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        jsonResult.set("size",size1);
        jsonResult.set("hotel",s);
        return jsonResult.getMap();
    }

    @ApiOperation(("房型详细信息"))
    @PostMapping("/detail")
    public Map selectDetail(String name){
        String o = roomsService.selectDetail(name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(o);
        return jsonResult.getMap();

    }

    @ApiOperation("生成订单进缓存,通知后台生成")
    @PostMapping("/order")
    public Map sendOrder(RoomOrder roomOrder, PayDetail payDetail){
        roomsService.sendorder(roomOrder,payDetail);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("订单发送成功");
        return      jsonResult.getMap();
    }

}
